package com.irdstudio.efp.esb.service.impl.dzqz;

import java.util.Objects;

import com.alibaba.fastjson.serializer.PascalNameFilter;
import com.irdstudio.efp.esb.common.util.JsonLogUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.basic.framework.core.util.StringUtil;
import com.irdstudio.efp.esb.common.client.resp.EsbRespServiceBean;
import com.irdstudio.efp.esb.common.constant.ElectronicSignatureConstant;
import com.irdstudio.efp.esb.common.constant.ElectronicSignatureEnums;
import com.irdstudio.efp.esb.service.bo.req.dzqz.ReqElectronicSignature3001Bean;
import com.irdstudio.efp.esb.service.bo.req.dzqz.ReqElectronicSignature3201Bean;
import com.irdstudio.efp.esb.service.bo.req.dzqz.ReqElectronicSignature3203Bean;
import com.irdstudio.efp.esb.service.bo.req.dzqz.ReqElectronicSignatureDL01Bean;
import com.irdstudio.efp.esb.service.bo.resp.dzqz.RspElectronicSignature3001Bean;
import com.irdstudio.efp.esb.service.bo.resp.dzqz.RspElectronicSignature3201Bean;
import com.irdstudio.efp.esb.service.bo.resp.dzqz.RspElectronicSignature3203Bean;
import com.irdstudio.efp.esb.service.bo.resp.dzqz.RspElectronicSignatureDL01Bean;
import com.irdstudio.efp.esb.service.client.ESBClientFactory;
import com.irdstudio.efp.esb.service.facade.dzqz.ElectronicSignatureService;

/**
 * 
 * @description:电子签章
 * @date:2018年12月15日 上午11:04:07
 * @author:ludayong
 */
@Service("electronicSignatureService")
public class ElectronicSignatureServiceImpl implements FrameworkService, ElectronicSignatureService {

	private static Logger logger = LoggerFactory.getLogger(ElectronicSignatureServiceImpl.class);

	/**
	 * 
	 * @description:电子签章个人开户3001
	 * @param req3001Bean
	 * @return
	 * @date:2018年12月15日 上午11:01:58
	 * @author:ludayong
	 */
	@Override
	public RspElectronicSignature3001Bean electronicSignaturePerOpenAccount(ReqElectronicSignature3001Bean req3001Bean)
			throws ESBException {
		logger.info("电子签章处理个人开户请求参数：" + JSONObject.toJSONString(req3001Bean));
		// 连接ESB请求电子签章个人开户接口
		RspElectronicSignature3001Bean rspBean = new RspElectronicSignature3001Bean();// 个人开户响应信息
		try {
			// 接收个人开户响应信息
			rspBean = ESBClientFactory.buildClient().withBody(req3001Bean).withTradeNo(ElectronicSignatureConstant.TRADE_NO_20120001)
					.withScene(ElectronicSignatureConstant.SCENE_04).create().sendAndReceive()
					.getBody(RspElectronicSignature3001Bean.class);
			logger.info("电子签章处理个人开户结束，返回信息：" + JSONObject.toJSONString(rspBean));
		} catch (ESBException e) {
			logger.error("电子签章处理个人开户出现异常：" + e.getMessage());
			throw new ESBException("电子签章处理个人开户出现异常：" + e.getMessage());
		}
		return rspBean;
	}

	/**
	 * 
	 * @description:创建合同签署3201
	 * @param req3201Bean
	 * @return
	 * @date:2018年12月19日 下午12:05:25
	 * @author:ludayong
	 */
	@Override
	public RspElectronicSignature3201Bean electronicSignatureSignCreateContract(ReqElectronicSignature3201Bean req3201Bean)
			throws ESBException {
		logger.info("电子签章处理创建合同签署请求参数：" + JSONObject.toJSONString(req3201Bean));
		// 连接ESB请求电子签章创建合同签署接口
		RspElectronicSignature3201Bean rspBean = new RspElectronicSignature3201Bean();// 创建合同签署响应信息
		try {
			// 创建合同签署响应信息
			rspBean = ESBClientFactory.buildClient().withBody(req3201Bean).withTradeNo(ElectronicSignatureConstant.TRADE_NO_30220008)
					.withScene(ElectronicSignatureConstant.SCENE_02).create().sendAndReceive()
					.getBody(RspElectronicSignature3201Bean.class);
			logger.info("电子签章处理：创建合同签署结束，返回信息：" + JSONObject.toJSONString(rspBean));
		} catch (ESBException e) {
			logger.error("电子签章处理创建合同签署出现异常：" + e.getMessage());
			throw new ESBException("电子签章处理创建合同签署出现异常：" + e.getMessage());
		}
		return rspBean;
	}

	/**
	 * 
	 * @description:上传合同签署3203
	 * @param req3203Bean
	 * @return
	 * @date:2018年12月15日 上午11:02:14
	 * @author:ludayong
	 */
	@Override
	public RspElectronicSignature3203Bean electronicSignatureSignUpContract(ReqElectronicSignature3203Bean req3203Bean)
			throws ESBException {
		String logJson = JSONObject.toJSONString(req3203Bean,new PascalNameFilter());
		logJson = JsonLogUtil.changeJSONStr(logJson,ElectronicSignatureConstant.KEY_CONTR_FL_STRM);
		logger.info("电子签章处理上传合同签署请求参数：" + logJson);
		// 连接ESB请求电子签章上传合同签署接口
		RspElectronicSignature3203Bean rspBean = new RspElectronicSignature3203Bean();// 上传合同签署响应信息
		// 错误响应信息
		String errorMsg = "";
		try {
			// 接收上传合同签署响应信息
			EsbRespServiceBean recvBean = ESBClientFactory.buildClient().withBody(req3203Bean)
					.withTradeNo(ElectronicSignatureConstant.TRADE_NO_30220008).withScene(ElectronicSignatureConstant.SCENE_01).create()
					.sendAndReceive();
			rspBean = recvBean.getBody(RspElectronicSignature3203Bean.class);
			try {
				Objects.requireNonNull(rspBean);
			} catch (Exception e) {
				// 错误响应信息
				errorMsg = recvBean.getSysHead().getRetInfArry().toString();
				throw e;
			}
			if (!StringUtil.isNullorBank(rspBean.getContract().getContrNo())) {
				rspBean.setRetStCd(ElectronicSignatureEnums.RetCodeEnum.SUCCESS.VALUE);// 处理成功
			} else {
				rspBean.setRetStCd(ElectronicSignatureEnums.RetCodeEnum.FAILURE.VALUE);// 处理失败
				rspBean.setRtnMsg(recvBean.getSysHead().getRetInfArry().toString());
			}
			logger.info("电子签章处理：上传合同签署结束，返回信息：" + JSONObject.toJSONString(rspBean));
		} catch (ESBException e) {
			logger.error("电子签章处理上传合同签署出现异常：" + e.getMessage());
			throw new ESBException("电子签章处理上传合同签署出现异常：" + e.getMessage() + "，响应信息：" + errorMsg);
		} catch (Exception e) {
			logger.error("电子签章处理上传合同签署出现异常：" + e.getMessage());
			throw new ESBException("电子签章处理上传合同签署出现异常：" + e.getMessage() + "，响应信息：" + errorMsg);
		}
		return rspBean;
	}

	/**
	 * 
	 * @description:合同下载DL01
	 * @param reqCADL01Bean
	 * @return
	 * @date:2018年12月15日 上午11:02:36
	 * @author:ludayong
	 */
	@Override
	public RspElectronicSignatureDL01Bean electronicSignatureDownloadContract(ReqElectronicSignatureDL01Bean reqCADL01Bean)
			throws ESBException {
		logger.debug("电子签章处理：合同下载请求参数：" + JSONObject.toJSONString(reqCADL01Bean));
		// 连接ESB请求电子签章合同下载接口
		RspElectronicSignatureDL01Bean rspBean = new RspElectronicSignatureDL01Bean();// 合同下载响应信息
		// 错误响应信息
		String errorMsg = "";
		try {
			// 接收上传合同签署响应信息
			EsbRespServiceBean recvBean = ESBClientFactory.buildClient().withBody(reqCADL01Bean)
					.withTradeNo(ElectronicSignatureConstant.TRADE_NO_30220003).withScene(ElectronicSignatureConstant.SCENE_02).create()
					.sendAndReceive();
			rspBean = recvBean.getBody(RspElectronicSignatureDL01Bean.class);
			try {
				Objects.requireNonNull(rspBean);
			} catch (Exception e) {
				// 错误响应信息
				errorMsg = recvBean.getSysHead().getRetInfArry().toString();
				throw e;
			}
			if (!StringUtil.isNullorBank(rspBean.getContrFlStrm())) {
				rspBean.setRetStCd(ElectronicSignatureEnums.RetCodeEnum.SUCCESS.VALUE);// 处理成功
			} else {
				rspBean.setRetStCd(ElectronicSignatureEnums.RetCodeEnum.FAILURE.VALUE);// 处理失败
				rspBean.setRtnMsg(recvBean.getSysHead().getRetInfArry().toString());
			}
			String logJson = JSONObject.toJSONString(rspBean,new PascalNameFilter());
			logJson = JsonLogUtil.changeJSONStr(logJson,ElectronicSignatureConstant.KEY_CONTR_FL_STRM);
			logger.info("电子签章处理：合同下载结束，返回信息：" + logJson);
		} catch (ESBException e) {
			logger.error("电子签章处理合同下载出现异常：" + e.getMessage());
			throw new ESBException("电子签章处理合同下载出现异常：" + e.getMessage() + "，响应信息：" + errorMsg);
		} catch (Exception e) {
			logger.error("电子签章处理合同下载出现异常：" + e.getMessage());
			throw new ESBException("电子签章处理合同下载出现异常：" + e.getMessage() + "，响应信息：" + errorMsg);
		}
		return rspBean;
	}

	/**
	 *
	 * @description:上传合同签署3203
	 * @param req3203Bean
	 * @return
	 * @date:2018年12月15日 上午11:02:14
	 * @author:ludayong
	 */
	@Override
	public RspElectronicSignature3203Bean electronicSignatureSignUpContract(ReqElectronicSignature3203Bean req3203Bean,String sceneNO)
			throws ESBException {
		String logJson = JSONObject.toJSONString(req3203Bean,new PascalNameFilter());
		logJson = JsonLogUtil.changeJSONStr(logJson,ElectronicSignatureConstant.KEY_CONTR_FL_STRM);
		logger.info("电子签章处理上传合同签署请求参数：" + logJson);
		// 连接ESB请求电子签章上传合同签署接口
		RspElectronicSignature3203Bean rspBean = new RspElectronicSignature3203Bean();// 上传合同签署响应信息
		// 错误响应信息
		String errorMsg = "";
		try {
			// 接收上传合同签署响应信息
			EsbRespServiceBean recvBean = ESBClientFactory.buildClient().withBody(req3203Bean)
					.withTradeNo(ElectronicSignatureConstant.TRADE_NO_30220008).withScene(sceneNO).create()
					.sendAndReceive();
			rspBean = recvBean.getBody(RspElectronicSignature3203Bean.class);
			try {
				Objects.requireNonNull(rspBean);
			} catch (Exception e) {
				// 错误响应信息
				errorMsg = recvBean.getSysHead().getRetInfArry().toString();
				throw e;
			}
			if (!StringUtil.isNullorBank(rspBean.getContract().getContrNo())) {
				rspBean.setRetStCd(ElectronicSignatureEnums.RetCodeEnum.SUCCESS.VALUE);// 处理成功
			} else {
				rspBean.setRetStCd(ElectronicSignatureEnums.RetCodeEnum.FAILURE.VALUE);// 处理失败
				rspBean.setRtnMsg(recvBean.getSysHead().getRetInfArry().toString());
			}
			logger.info("电子签章处理：上传合同签署结束，返回信息：" + JSONObject.toJSONString(rspBean));
		} catch (ESBException e) {
			logger.error("电子签章处理上传合同签署出现异常：" + e.getMessage());
			throw new ESBException("电子签章处理上传合同签署出现异常：" + e.getMessage() + "，响应信息：" + errorMsg);
		} catch (Exception e) {
			logger.error("电子签章处理上传合同签署出现异常：" + e.getMessage());
			throw new ESBException("电子签章处理上传合同签署出现异常：" + e.getMessage() + "，响应信息：" + errorMsg);
		}
		return rspBean;
	}

	/**
	 *
	 * @description:上传合同签署3203
	 * @param req3203Bean
	 * @return
	 * @date:2020年8月13日08:49:51
	 * @author:zhangxican
	 */
	@Override
	public RspElectronicSignature3203Bean electronicSignatureSignUpContract1(ReqElectronicSignature3203Bean req3203Bean, String sceneNO)
			throws ESBException {
		String logJson = JSONObject.toJSONString(req3203Bean,new PascalNameFilter());
		logJson = JsonLogUtil.changeJSONStr(logJson,ElectronicSignatureConstant.KEY_CONTR_FL_STRM);
		logger.info("电子签章处理上传合同签署请求参数：" + logJson);
		// 连接ESB请求电子签章上传合同签署接口
		RspElectronicSignature3203Bean rspBean = new RspElectronicSignature3203Bean();// 上传合同签署响应信息
		// 错误响应信息
		String errorMsg = "";
		try {
			// 接收上传合同签署响应信息
			EsbRespServiceBean recvBean = ESBClientFactory.buildClient().withBody(req3203Bean)
					.withTradeNo(ElectronicSignatureConstant.TRADE_NO_30220008).withScene(sceneNO).create()
					.sendAndReceive();
			rspBean = recvBean.getBody(RspElectronicSignature3203Bean.class);
			try {
				Objects.requireNonNull(rspBean);
			} catch (Exception e) {
				// 错误响应信息
				errorMsg = recvBean.getSysHead().getRetInfArry().toString();
				throw e;
			}
			if (!StringUtil.isNullorBank(rspBean.getContract().getContrNo())) {
				rspBean.setRetStCd(ElectronicSignatureEnums.RetCodeEnum.SUCCESS.VALUE);// 处理成功
			} else {
				rspBean.setRetStCd(ElectronicSignatureEnums.RetCodeEnum.FAILURE.VALUE);// 处理失败
				rspBean.setRtnMsg(recvBean.getSysHead().getRetInfArry().toString());
			}
			logger.info("电子签章处理：上传合同签署结束，返回信息：" + JSONObject.toJSONString(rspBean));
		} catch (ESBException e) {
			logger.error("电子签章处理上传合同签署出现异常：" + e.getMessage());
			throw new ESBException("电子签章处理上传合同签署出现异常：" + e.getMessage() + "，响应信息：" + errorMsg);
		} catch (Exception e) {
			logger.error("电子签章处理上传合同签署出现异常：" + e.getMessage());
			throw new ESBException("电子签章处理上传合同签署出现异常：" + e.getMessage() + "，响应信息：" + errorMsg);
		}
		return rspBean;
	}
}
